// instanceof 判断你是否由这个东西new出来的例子如下
// 原型链的顶端是Object
let arr = []; // new Array
console.log(arr instanceof Array);

function User() {}
let jason = new User();
console.log(jason instanceof User);

let hd = 'hodunren"ddddd';
console.log(hd);

let year = "2010年";
let site = "houdunren";
console.log(`${site} 成立于 ${year}`);

let myName = " houDUNren";
console.log(myName.length);

console.log(myName.toUpperCase());
console.log(myName.toLowerCase());
console.log(myName.trim().length);
console.log(myName.charAt(1)); // h

let hxxd = "houdunren.com";
console.log(hxxd.slice(1, 3)); // 推荐使用这个 从一开始截取 就是h，然后截取到3 所以就只输出ou
console.log(hxxd.substring(1, 3)); // 负数没有意义
console.log(hxxd.substr(-3, 2)); // 已经废弃不推荐使用

const word = ["php", "css"];
const string = "我喜欢在后盾人学习php和css知识"; // 一把查找字符串是否存在用 includes是比较好的，会比较简单的返回true或者false

const findWord = word.some((word) => {
  // some 当你找到了一个之后整个就返回了true，就不会接着往下找
  return string.includes(word);
});

if (findWord) console.log(`找到了关键字了`);

function phone(mobile, len) {
  // 截取倒数的len位 ，然后+* ，repeat是复制，传入len 复制3个
  return String(mobile).slice(0, len * -1) + "*".repeat(len);
}
console.log(phone(13189030604, 3));

const stringone = "99";
console.log(typeof stringone);
console.log(+stringone + 78); // 隐式转换
console.log(Number(stringone) + 88);

const num = 66;
console.log(typeof num);
const str = num + "";
console.log(typeof str);
console.log(typeof String(num)); // 数字转换为字符串

const strNum = "99.23induwan";
console.log(parseFloat(strNum)); // 保留浮点型， parseInt保留整型

const arro = ["jaosn", "michearl", "firday"];
arro.splice(1, 0, "Feb"); // 第一个元素 删除0个元素， 插入 "Feb"
console.log(arro);

const cms = "hdcsm";
console.log(cms.split("").length); // 5
console.log(cms.split("")); // 将字符串转数组

const dataArray = ["joradna", "michael", "kobe"];
console.log(dataArray.join(" |")); // 用｜分割成字符串

const weekArray = ["monday", "fiebday", "thirday"];
console.log(weekArray.toString()); //将数组转成字符串

let array = [];
let obj = {};
console.log(Boolean(array)); // 空数组Boolean 是 true
console.log(Boolean(obj)); // 空对象Boolean 是 true

if ({}) console.log("hdcms");

let numberOne = 99.156;
console.log(numberOne.toFixed(2)); // 小数点后留两位,可以输入1 2 3...

console.log(Number("houdunren"));
console.log(2 / "houdunren");
console.log(NaN == NaN);
console.log(Number.isNaN(2 / "houdunren"));
console.log(Object.is(2 / "houdunren", NaN));

let arrayOne = [1, 2];
console.log(Number(arrayOne)); // 数组只有1个时候是当中的数字，数组中有多个就是NaN了
console.log(Number({})); // NaN

// Math 数学函数
console.log(Math.min(1, 2, 3, 4, 5)); // 1
let grade = [12, 3, 2, 123];
console.log(Math.max(grade)); // NaN
console.log(Math.max.apply(null, grade)); // apply call 相当于 Math.max(12, 3, 2, 123)

console.log(Math.ceil(5.01)); // 向上取整, ceil天花板 6
console.log(Math.floor(5.99)); // 向下取整, floor地板 5
console.log(Math.round(5.5)); // 四舍五入

// 随机数  0<= x < 1
// Math.floor(Math.random() * (Max + 1))
console.log(Math.floor(Math.random() * (5 + 1)));

const students = ["李四", "张三", "王五", "老刘", "老七"];

function arrayRandomValue(array, start = 1, end) {
  end = end ? end : array.length;
  start--;
  const index = start + Math.floor(Math.random() * (end - start));
  return array[index];
}

console.log(arrayRandomValue(students, 3, 5));

// Date 日期
const date = new Date(); // 2023-12-06T02:08:59.959Z
console.log("日期 + 8", date);

const date1 = Date();
console.log("GMT时间China", date1);

// const start1 = Date.now();
// for (let i = 0; i < 10000000000; i++) {}
// const end1 = Date.now();
// console.log((end1 - start1) / 1000 + "秒");

// 打个标签记录时间
// console.time("for");
// for (let i = 0; i < 1000; i++) {}
// console.timeEnd("for");

const param = [2021, 2, 22, 13, 22, 19]; // 2021年 3月份 22日 13:22:19
const date2 = new Date(...param);
console.log(date2);

const date3 = new Date("1996-7-12 08:22:13");
const timestamp = date3.valueOf(); // 获得毫秒时间
console.log(new Date(timestamp)); // 重新转换为标准时间

const date4 = new Date("2023-12-6 10:30:19");
console.log(date4);
console.log(date4.getFullYear());
console.log(date4.getMonth() + 1);
console.log(date4.getDate());
console.log(date4.getHours());
console.log(date4.getMinutes());
console.log(date4.getSeconds());
console.log(date4.getDay()); // 获得星期几

function dateFormat(date, format = "YYYY-MM-DD HH:mm:ss") {
  const config = {
    YYYY: date.getFullYear(),
    MM: date.getMonth(),
    DD: date.getDate(),
    HH: date.getHours(),
    mm: date.getMinutes(),
    ss: date.getSeconds(),
  };

  for (const key in config) {
    format = format.replace(key, config[key]);
  }

  return format;
}

console.log(dateFormat(date4, "YYYY年MM月DD日"));
